<HTML><HEAD><TITLE>flatten(+NestedList, ?FlatList)</TITLE>
</HEAD><BODY>[ <A HREF="index.html">library(lists)</A> | <A HREF="../../index.html">Reference Manual</A> | <A HREF="../../fullindex.html">Alphabetic Index</A> ]
<H1>flatten(+NestedList, ?FlatList)</H1>
Succeeds if FlatList is the list of all elements in NestedList, as found in
a left-to-right, depth-first traversal of NestedList.


<DL>
<DT><EM>+NestedList</EM></DT>
<DD>Ground List.
</DD>
<DT><EM>?FlatList</EM></DT>
<DD>List or variable.
</DD>
</DL>
<H2>Description</H2>
   FlatList is the list built from all the non-list elements of NestedList
   and the flattened sublists.  The sequence of elements in FlatList is
   determined by a left-to-right, depth-first traversal of NestedList.
<P>
   The definition of this Prolog library predicate is:
<PRE>
flatten(List, Flat) :-
	flatten_aux(List, Flat, []).

flatten_aux([], Res, Cont) :- -?-> !, Res = Cont.
flatten_aux([Head|Tail], Res, Cont) :-
	-?->
	!,
	flatten_aux(Head, Res, Cont1),
	flatten_aux(Tail, Cont1, Cont).
flatten_aux(Term, [Term|Cont], Cont).
</PRE>
   This predicate does not perform any type testing functions.
	
<H3>Modes and Determinism</H3><UL>
<LI>flatten(+, -) is det
</UL>
<H3>Fail Conditions</H3>
   Fails if FlatList does not unify with the flattened version of
   NestedList.


<H3>Resatisfiable</H3>
   No.
<H2>Examples</H2>
<PRE>
Success:
    [eclipse]: flatten([[1,2,[3,4],5],6,[7]], L).
    L = [1, 2, 3, 4, 5, 6, 7]
    yes.

Fail:
    [eclipse]: flatten([1,[3],2], [1,2,3]).
    no.



</PRE>
<H2>See Also</H2>
<A HREF="../../lib/lists/flatten-3.html">flatten / 3</A>, <A HREF="../../kernel/termcomp/sort-2.html">sort / 2</A>, <A HREF="../../kernel/termcomp/sort-4.html">sort / 4</A>, <A HREF="../../lib/lists/length-2.html">length / 2</A>, <A HREF="../../lib/lists/member-2.html">member / 2</A>
</BODY></HTML>
